//
//  PSPageControl.m
//  ZAYY
//
//  Created by Vic on 2018/9/10.
//  Copyright © 2018年 Vic. All rights reserved.
//

#import "PSPageControl.h"

@implementation PSPageControl

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat dotW = 15*Scale;
    CGFloat dotH = 7*Scale;
    CGFloat dotM = 10*Scale;
    
    CGFloat marginX = dotH + dotM;
    CGFloat newW = (self.subviews.count - 1) * marginX+dotW;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, newW, self.frame.size.height);
//    CGPoint center = self.center;
//    center.x = self.superview.center.x;
//    self.center = center;
    
    for (NSInteger i = 0; i < self.subviews.count; i++) {
        UIImageView *dot = self.subviews[i];
        if (i == self.currentPage) {
            dot.frame = CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotH);
        } else {
            if (i == 0) {
                dot.frame = CGRectMake(0, dot.frame.origin.y, dotH, dotH);
            } else if (i<self.currentPage) {
                dot.frame = CGRectMake(i * marginX, dot.frame.origin.y, dotH, dotH);
            } else {
                dot.frame = CGRectMake((i-1) * marginX + dotW + dotH, dot.frame.origin.y, dotH, dotH);
            }
        }
        dot.layer.cornerRadius = 3.5*Scale;
        dot.layer.masksToBounds = YES;
    }
}

@end
